<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="showRoleDialog"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="roleList">
          <el-table-column label="序号" width="120" type="index" :index="index" />
          <el-table-column label="角色名称" width="240" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作" width="240" fixed="right">
            <template #default="{row}">
              <el-button size="small" @click="assignRole(row.id)">分配权限</el-button>
              <el-button size="small" @click="updata(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            layout="prev,pager,next"
            :total="total"
            :page-size="params.pagesize"
            @current-change="currentChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog title="编辑弹层" :visible="roleDialogVisible" @close="closeRoleDialog">
      <!-- 表单内容 -->
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-button size="small" @click="reset">取消</el-button>
        <el-button size="small" type="primary" @click="addRole">确定</el-button>
      </el-row>
    </el-dialog>
    <!-- 权限弹窗 -->
    <template>
      <el-dialog
        class="assign-role"
        title="分配角色"
        :visible="showAssignDialog"
        @open="cpenAssignDialog"
        @close="colesAssignDialog"
      >
        <!-- 权限点数据展示 -->
        <template #default>
          <el-tree
            ref="tree"
            :data="permissionData"
            :props="{ label: 'name' }"
            :default-expand-all="true"
            :show-checkbox="true"
            :check-strictly="true"
            node-key="id"
          />
        </template>
        <!-- 这里准备复选框 -->
        <template #footer>
          <el-button size="small">取消</el-button>
          <el-button type="primary" size="small" @click="confirmAssign">确定</el-button>
        </template>
      </el-dialog>
    </template>
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole, assignPerm, getRoleDetailById } from '@/api/setting'
import { getPermissionList } from '@/api/permisson'
import transTree from '@/utils/transTree'
export default {
  data() {
    return {
      roleList: [],
      params: {
        page: 1,
        pagesize: 5
      },
      total: 0,
      roleDialogVisible: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      showAssignDialog: false,
      permissionData: [],
      curId: null
    }
  },
  mounted() {
    this.fetchList()
  },
  methods: {
    // 索引方法
    index(index) {
      index++
      return index * 2
    },
    // 刷新页面数据列表
    async fetchList() {
      const res = await getRoleList(this.params)
      this.roleList = res.rows
      this.total = res.total
    },
    // 页数发生改变的时候
    currentChange(page) {
      console.log(page)
      this.params.page = page
      this.fetchList()
    },
    // 删除数据
    async delRole(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteRole(id)
        this.fetchList()
        this.$message.success('操作成功')
      }).catch(() => {
        return false
      })
    },
    // 打开弹窗
    showRoleDialog() {
      this.roleDialogVisible = true
    },
    // 关闭弹窗
    closeRoleDialog() {
      // 弹窗关闭
      this.roleDialogVisible = false
      // 清除表单收集内容，和表单的验证信息
      this.$refs.roleForm.resetFields()
      // 数据恢复
      this.roleForm = {
        neme: '',
        description: ''
      }
    },
    // 弹窗里的确认按钮
    addRole() {
      this.$refs.roleForm.validate(async(valid) => {
        if (valid) {
          if (this.roleForm.id) {
            await updateRole(this.roleForm)
          } else {
            // 调用新增接口
            await addRole(this.roleForm)
          }
          // 刷新
          this.fetchList()
          // 提示用户
          this.$message.success(`${this.roleForm.id ? '编辑角色' : '新增角色'}`)
          // 关闭弹窗
          this.closeRoleDialog()
        }
      })
    },
    // 弹窗里的取消按钮
    reset() {
      this.closeRoleDialog()
    },
    // 编辑功能
    async updata(id) {
      // 数据回填
      const res = await getRoleDetail(id)
      this.roleForm = res
      // 打开弹窗
      this.roleDialogVisible = true
    },
    assignRole(id) {
      this.showAssignDialog = true
      this.curId = id
    },
    colesAssignDialog() {
      this.showAssignDialog = false
    },
    async cpenAssignDialog() {
      const res = await getPermissionList()
      this.permissionData = transTree(res)

      // 获取详情数据
      const detail = await getRoleDetailById(this.curId)
      // 拿到的数据回显可选列表中
      this.$refs.tree.setCheckedKeys(detail.permIds)
    },
    confirmAssign() {
      assignPerm({
        id: this.curId,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      // 提示用户
      this.$message.success('分配权限点成功')
      // 关闭弹框
      this.colesAssignDialog()
    }
  }
}
</script>

<style>

</style>
